package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.ApplicantExam;
import com.bosssoft.boss_exam_db.mapper.ApplicantExamMapper;
import com.bosssoft.boss_exam_db.model.vo.ApplicantExamVO;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.ApplicantExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ApplicantExamServiceImpl extends BaseServiceImpl<ApplicantExam> implements ApplicantExamService {

    //注入ExamMapper
    @Autowired
    private ApplicantExamMapper applicantExamMapper;


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 插入应聘人考试记录
     * @Param [examId, applicantId] 考试ID  应聘人ID
     * @Return int
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public int insertApplicantExam(String examId, String applicantId) {
        Example example = new Example(ApplicantExam.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(examId)){
            return 0;
        }
        if(StringUtils.isEmpty(applicantId)){
            return 0;
        }
        // 报名前先查看是否已经报名过了，规避重复报名的业务错误
        criteria.andEqualTo("applicantId",applicantId);
        criteria.andEqualTo("examId",examId);
        criteria.andEqualTo("deleted",false);
        List<ApplicantExam> applicantExams = this.applicantExamMapper.selectByExample(example);
        if(applicantExams!=null && applicantExams.size()!=0){
            return 0;
        }
        ApplicantExam applicantExam = new ApplicantExam();
        applicantExam.setExamId(examId);
        applicantExam.setApplicantId(applicantId);
        applicantExam.setAttendTime(new Date());
        int record = this.applicantExamMapper.insertSelective(applicantExam);
        return record;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/31
     * @Description 根据应聘人查询试卷
     * @Param [applicantId] 应聘人ID
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.Exam>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<ApplicantExamVO> queryExamByApplicant(String applicantId) {
        List<ApplicantExamVO> exams = this.applicantExamMapper.queryByApplicantId(applicantId);
        return exams;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 查询应聘人考试记录
     * @Param [allowExamFlag, page, limit, sort, order]
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.model.vo.ApplicantExamVO>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<ApplicantExamVO> queryByCondition(Integer allowExamFlag, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit).setOrderBy("applicant_exam."+sort + " " + order);
        List<ApplicantExamVO> applicantExamExamVOS = this.applicantExamMapper.queryAllApplicantExam(allowExamFlag);
        PageInfo<ApplicantExamVO> applicantExamExamVOPageInfo = new PageInfo<>(applicantExamExamVOS);
        return applicantExamExamVOPageInfo;
    }


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 更新应聘人记录 是否允许考试
     * @Param [applicantExams, allowExamFlag]
     * @Return int
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public int updatebath(List<ApplicantExam> applicantExams,Integer allowExamFlag) {
        List<ApplicantExam> applicantExamList = applicantExams.stream().map(item -> {
            ApplicantExam applicantExam = new ApplicantExam();
            applicantExam.setId(item.getId());
            applicantExam.setAllowExam(allowExamFlag);
            return applicantExam;
        }).collect(Collectors.toList());
        try{
            applicantExamList.forEach(item->{
                this.applicantExamMapper.updateByPrimaryKeySelective(item);
            });
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
